<?php
/**
 * Date: 2017/11/10
 * Time: 10:08
 * Author: zeronlee
 */

namespace App\Http\Controllers\Backend\Email;


use App\Http\Controllers\Controller;
use App\Models\Flaw\Vuls;
use App\Models\App;
use App\Services\Vuls\SendEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use  Illuminate\Database\Query\Builder;
use App\Models\Flaw\Task;

class EmailController extends Controller
{


    /**
     * 发送邮件
     */
    public function sendto()
    {
        //允许用户主动触发,不能限制为本地执行，故通过权限控制
        /*if ('127.0.0.1' != getClientIp()) {
            Log::info('send email only by localhost.');
            exit();
        }*/
        $tasks = Task::getList();
        $records = [];
        if ($tasks->isEmpty()) {
            Log::info('send email task table empty.');
            exit();
        }
        foreach ($tasks as $task) {
            switch ($task->sendtype) {
                case SendEmail::ASSIGN:
                    {
                        if (in_array($task->status,[Vuls::BECONFIRM,Vuls::BEREPAIRE])) {

                            $records[$task->appid][SendEmail::ASSIGN][] = $task;
                        }
                        break;
                    }
                case SendEmail::ISTIMEOUT:
                    {
                        if (in_array($task->status, [Vuls::BECONFIRM, Vuls::BEREPAIRE])) {

                            $records[$task->appid][SendEmail::ISTIMEOUT][] = $task;
                        }
                        break;
                    }
                case SendEmail::HASTIMEOUT:
                    {
                        if (in_array($task->status, [Vuls::BECONFIRM, Vuls::BEREPAIRE])) {

                            $records[$task->appid][SendEmail::HASTIMEOUT][] = $task;
                        }
                        break;
                    }
                case SendEmail::SHUTDOWN:
                    {
                        if (in_array($task->status,[Vuls::SOLVED,Vuls::CLOSED])) {

                            $records[$task->appid][SendEmail::SHUTDOWN][] = $task;
                        }
                        break;
                    }
            }
        }
        $service = new SendEmail();
        $emailData = [
            'recive' => '海航集团信息安全应急响应中心',
            'connect' => '',
            'APP_ROOT' => env('APP_URL') . '/' . env('APP_ROOT') . '/',
            'currentTime' => date('Y年m月d日'),
            'time' => getStrTime()
        ];
        foreach ($records as $appId => $item){
            $appInfo = Task::select(['appname','useremail','user'])->where(['appid'=>$appId])->first();
            //邮件主题
            $emailData['from'] = $appInfo->appname;
            //邮件非正文相关信息，每个应用一份信息
            $service->setInfo($emailData);
            //内容中的发件人信息
            $service->sendUsers($appInfo->useremail,$appInfo->user);
            $service->receiver($appInfo->useremail,$appInfo->user);
            $service->subject = $appInfo->appname;
            //审核确认后发送邮件
            if(isset($item[SendEmail::ASSIGN])){
                $service->audit($item[SendEmail::ASSIGN]);
            }
            //即将超时
            if(isset($item[SendEmail::ISTIMEOUT])){
                $service->type = SendEmail::ISTIMEOUT;
                $service->isTimeOut($item[SendEmail::ISTIMEOUT]);
            }
            //已经超时
            if(isset($item[SendEmail::HASTIMEOUT])){
                $service->type = SendEmail::HASTIMEOUT;
                $service->hasTimeOut($item[SendEmail::HASTIMEOUT]);
            }
            //关闭
            if(isset($item[SendEmail::SHUTDOWN])){
                $service->shutdown($item[SendEmail::SHUTDOWN]);
            }
            //重置基础公共信息
            $service->setInfo();
        }

    }

}